﻿using System;
using System.Linq;
using System.Collections.Generic;

using Keyki.NefClass.Kernel.Data;

namespace Keyki.NefClass.Kernel.Fuzzy.Rulles
{
	public class Antecedent
	{
		private List<LinguisticVariable> antecedent;

		public Antecedent()
		{
			antecedent = new List<LinguisticVariable>();
		}

		public int Count
		{
			get { return antecedent.Count; }
		}//Count

		public LinguisticVariable this[int index]
		{
			get
			{
				if (index >= antecedent.Count)
					throw new ArgumentOutOfRangeException("No such antecedent");

				return antecedent[index];
			}//get
			set
			{
				if (index >= antecedent.Count)
					antecedent.Add(value);
				
				antecedent[index] = value;
			}//set
		}//this[]

		public int ArgMin { get; private set; }

		public double Compute(DataSample value)
		{
			if (value.Count != antecedent.Count)
				throw new ArgumentException("Keyki.NefClass.Kernel.Fuzzy.FuzzyRule.Compute(): Wrong numbers of argumets");
			
			double result = double.MaxValue;
			double tmpValue;

			for (int i = 0; i < antecedent.Count; i++)
			{
				var variable = antecedent[i];
				tmpValue = variable.FuzzySet.Compute(value[i]);

				if (tmpValue < result)
				{
					ArgMin = i;
					result = tmpValue;
				}
			}//for is

			return result;
		}//Compute

		public override bool Equals(object obj)
		{
			if (!(obj is Antecedent))
				return false;

			var ant = (Antecedent) obj;

			bool result = true;

			for (int i = 0; i < antecedent.Count; i++)
			{
				var variable = antecedent[i];
				result = variable.FuzzySet.Equals(ant[i].FuzzySet);

				if (!result)
				{
					break;
				}
			}//for

			return result;
		}//Equals

		public override int GetHashCode()
		{
			return base.GetHashCode();
		}//GetHashCode

	}//Antecedent
}
